package app;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Class to manage application configure data
 * 
 * @author Lukas Vavra
 *
 */
public class AppConfig {
	private static final String PATH = "settings";
	
	private static final String SERVER = "server";
	private static final String TOKEN = "token";
	private static final String LASTPATH = "path";
	private static final String USRNAME = "name";
	private static final String SAVELOGIN = "savelogin";
			
	private String server = null;
	private String token = null;
	private String lastpath = null;
	private String name = null;
	private boolean savelogin = false;
	
	private File setup;

	public AppConfig() {
		setup = new File(PATH);
		if(!(setup.exists() && setup.isFile())) {
			try {
				setup.createNewFile();
			} catch (IOException e) {
				Main.showError("Systémová chyba", "Nelze vytvořit ukládací soubor pro nastavení aplikace");
				System.exit(-1);
			}
		}
		load();
	}
	
	public void load() {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(setup));
			String line;
			do {
				line = reader.readLine();
				if(line == null)
					break;
				String[] s = line.split(" ", 2);
				if(s[0].equals(LASTPATH)) {
					setLastpath(s[1]);
				}
				else if(s[0].equals(SERVER)) {
					setServer(s[1]);
				} 
				else if(s[0].equals(TOKEN)) {
					setToken(s[1]);
				}
				else if(s[0].equals(USRNAME)) {
					setName(s[1]);
				}
				else if(s[0].equals(SAVELOGIN)) {
					setSavelogin(s[1].equals("true"));
				}
			} while(line != null);
			reader.close();
		} catch (IOException e) {
			Main.showError("Systémová chyba", "Nelze otevřít ukládací soubor pro nastavení aplikace");
			System.exit(-1);
		}
	}
	
	public void save() {
		try {
			PrintWriter writer = new PrintWriter(setup);
			
			writer.println(SAVELOGIN.concat(" ").concat(isSavelogin() ? "true" : "false"));
			
			if(getLastpath() != null) writer.println(LASTPATH.concat(" ").concat(getLastpath()));
			if(getServer() != null) writer.println(SERVER.concat(" ").concat(getServer()));
			if(getToken() != null) writer.println(TOKEN.concat(" ").concat(getToken()));
			if(getName() != null) writer.println(USRNAME.concat(" ").concat(getName()));
			
			writer.close();
		} catch (FileNotFoundException e) {
			Main.showError("Systémová chyba", "Nelze otevřít ukládací soubor pro nastavení aplikace");
			System.exit(-1);			}
	}

	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getLastpath() {
		return lastpath;
	}

	public void setLastpath(String lastpath) {
		this.lastpath = lastpath;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isSavelogin() {
		return savelogin;
	}

	public void setSavelogin(boolean savelogin) {
		this.savelogin = savelogin;
	}
}
